<?php

    class Roles_Controller extends Application {
        var $roles;
        
        function setup()
        {
            $this->actions = array('index','modify','modify_user_roles','modify_role_users','delete');
            $this->use_layout('admin_layout.php');
            $this->before_filter('is_logged_in');
            $this->before_filter('has_access');
            use_model('roles','users','acl');
            $this->roles = new Roles();
			
			// set subnav to template
			$this->assign('subnav',admin_subnav());
        }
        
        function has_access()
        {
            if (!has_access_to('Admin::Modify Roles')) {
                flash('error','Sorry but you do not have access to the Roles tool.');                
                $this->redirect_to(array('controller'=>'admin/dashboard'));
            }
        }
        
		function index() 
		{
		    
	        $this->roles = new Roles();
	        $this->assign('all_roles',$this->roles->get_roles_for_select());
	        $this->assign('role_descriptions',$this->roles->find("1 order by role_name"));
	        
		    // model
		    $this->assign_ref('roles',$this->roles);
		    
		    // add role
		    if ( isset($_POST['action']) && $_POST['action'] == 'add_role' )
		    {
		        if ( $this->roles->update_fields($_POST['roles']) )
		        {
		            flash('notice','Role Added');
		            $this->redirect_to(array('controller'=>'admin/roles' ,'action'=>'index?id='.$this->roles->get('id')));
		        }
		    }
		    
		    // edit role
		    if ( isset($_POST['action']) && $_POST['action'] == 'edit_role' )
		    {
		        if ( $this->roles->load( (int) $_POST['roles']['id']) 
		            && $this->roles->update_fields($_POST['roles']) )
		        {
		            flash('notice','Role Edited');
		            $this->redirect_to(array('controller'=>'admin/roles' ,'action'=>'index?id='.$this->roles->get('id')));
		        } else {
                    $id = $_POST['roles']['id'];
		        }
		    }
		    
		    // assign role stuff
		    $role_id = isset($id) ? $id : (int) @$_GET['id'];
		    if ( !empty($role_id) )
		    {
		         // user details
    		     $this->roles->load($role_id);

		         // current role
		         $this->assign('role',$this->roles->fields);

    	         // acls
    	         $acl = new Acl();
    	         $this->assign('all_acls', $acl->get_all_acls());

		         // get users
		         $users = new Users();
		         $this->assign('all_users',$users->get_users_as_list());
		         $this->assign('role_users',$this->roles->get_role_users($role_id));
		         
		         // user permissions
		         $this->assign('role_acls',$acl->get_role_access($role_id));
		    }
		}
		
		function modify()
        {
            if ( $_POST && !empty($_POST['id']) )
            {
                $this->roles->load( (int) $_POST['id'] );
                $this->roles->update_fields($_POST['roles']);
                $this->render(array('inline'=>'Success: Role Modified'));
                return;
            }            
            $this->redirect_to('index');
        }
        
        function modify_user_roles()
        {
            if ( $_POST )
            {
                $user_id = (int) $_POST['user_id'];
                $roles   = $_POST['roles'];
                
                // loop through roles and add them.
                if ( count($roles) > 0 )
                {
                    $this->roles->clear_user_roles($user_id);
                    foreach( $roles as $role_id )
                    {
                        $this->roles->put_user_in_role($role_id,$user_id);
                    }
                    $this->render(array('inline'=>'Success: Roles Updated'));
                    return;
                } else {
                    $this->roles->clear_user_roles($user_id);
                    $this->render(array('inline'=>'Success: Roles Changed'));
                    return;
                }
            }
            $this->redirect_to('index');
        }
        
        function modify_role_users()
        {
            if ( $_POST )
            {
                $users   = $_POST['users'];
                $role_id = (int) $_POST['role_id'];
                
                // loop through roles and add them.
                if ( count($users) > 0 )
                {
                    $this->roles->clear_role_users($role_id);
                    foreach( $users as $user_id )
                    {
                        $this->roles->put_user_in_role($role_id,$user_id);
                    }
                    $this->render(array('inline'=>'Success: Role Updated'));
                    return;
                } else {
                    $this->roles->clear_role_users($role_id);
                    $this->render(array('inline'=>'Success: Users Changed'));
                    return;
                }
            }
            $this->redirect_to('index');
        }
        
        function delete()
        {
            if ( isset($_POST['action']) 
                 && $_POST['action'] == 'delete' 
                 && !empty($_POST['id']) )
            {
                $this->roles->load( (int) $_POST['id'] );
                if (!$this->roles->delete())
                {                    
                    flash('error','Delete of role failed');
                } else {
                    flash('notice','Role Deleted');
                }
            }
            $this->redirect_to('index');
        }

        
    }

?>